package com.tianji.promotion.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.promotion.domain.dto.CouponFormDTO;
import com.tianji.promotion.domain.dto.CouponIssueFormDTO;
import com.tianji.promotion.domain.query.CouponQuery;
import com.tianji.promotion.domain.vo.CouponDetailVO;
import com.tianji.promotion.domain.vo.CouponPageVO;
import com.tianji.promotion.domain.vo.CouponVO;
import com.tianji.promotion.service.ICouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 优惠券的规则信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-03-28
 */
@Slf4j
@RestController
@RequestMapping("/coupons")
@Api(tags = "优惠券相关接口")
@RequiredArgsConstructor
public class CouponController {
    private final ICouponService couponService;

    @PostMapping
    @ApiOperation("创建优惠券")
    public void createCoupon(@RequestBody @Valid CouponFormDTO couponDTO) {
        couponService.saveCoupon(couponDTO);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询优惠券")
    public PageDTO<CouponPageVO> queryCouponPage(CouponQuery query) {
       return couponService.queryCouponPage(query);
    }

    @PutMapping("/{id}/issue")
    @ApiOperation("更新优惠券")
    public void updateCoupon(@RequestBody @Valid CouponIssueFormDTO couponDTO) {
        couponService.beginIssue(couponDTO);
    }

    @PutMapping("/{id}")
    @ApiOperation("修改优惠券")
    public void updateCoupon(@RequestBody @Valid CouponFormDTO couponDTO) {
        couponService.updateCoupon(couponDTO);
    }


    @DeleteMapping("/{id}")
    @ApiOperation("删除优惠券")
    public void deleteCoupon(@PathVariable Long id) {
        couponService.removeCoupon(id);
    }

    @GetMapping("/{id}")
    @ApiOperation("查询优惠券")
    public CouponDetailVO getCoupon(@PathVariable Long id) {
        return couponService.findById(id);
    }


    @GetMapping("/list")
    @ApiOperation("查询用户优惠券列表")
    public List<CouponVO> queryIssuingCoupons() {
        return couponService.queryIssuingCoupons();
    }

}
